﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Catering.Catering_Page.system
{
    public partial class FocusImageManage : System.Web.UI.Page
    {
        public int m_UpState = 0;//0:尚未上传图片、1：上传文件类型错误、2：上传成功进入预览、3：没有上传文件
        public string m_ImageYL = "";
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string path = null;
            string fileextenison = string.Empty;
            string newimgname = Dr_Image.SelectedValue;
            HF_Save.Value = "";
            if (Imgfile.HasFile)
            {
                path = Imgfile.PostedFile.FileName.ToString();
                fileextenison = System.IO.Path.GetExtension(path);
                if (fileextenison != ".jpg")
                {
                    m_UpState = 1;
                }
                else
                {
                    path = System.Web.HttpContext.Current.Server.MapPath(@"~/UpFile/" + newimgname);//保存文件地址
                    if (System.IO.File.Exists(path))//如果存在删除
                    {
                        File.Delete(path);
                    }
                    Imgfile.SaveAs(path);//保存
                    HF_Save.Value = newimgname;
                    Dr_Image1.SelectedIndex = Dr_Image.SelectedIndex;
                    m_ImageYL = "UpFile/" + newimgname;
                    m_UpState = 2;
                }
            }
            else
            {
                m_UpState = 3;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string newimgname = HF_Save.Value;
            if (!string.IsNullOrEmpty(newimgname))
            {
                string path = System.Web.HttpContext.Current.Server.MapPath(@"~/UpFile/" + newimgname);//保存文件地址
                string deletepath = System.Web.HttpContext.Current.Server.MapPath(@"~/Images/" + newimgname);
                if (System.IO.File.Exists(deletepath))//如果存在删除
                {
                    File.Delete(deletepath);
                }
                if (System.IO.File.Exists(path))
                {
                    File.Move(path, deletepath);
                }
                Response.Write("<script>alert('保存成功！');location.href='FocusImageManage.aspx';</script>");
            }
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Write("<script>location.href='FocusImageManage.aspx';</script>");
        }
    }
}